package net.neiquan.neiquandemowithkf.fragment;

import net.neiquan.neiquandemowithkf.adapter.PinyinAdapter_;
import net.neiquan.neiquandemowithkf.entity.AfterUser;
import net.neiquan.neiquandemowithkf.entity.MyUser;
import net.neiquan.neiquandemowithkf.entity.UserLetterUtils;
import net.neiquanlib.base.BaseListViewFragment;
import net.neiquanlib.base.MyBaseAdapterListView;
import net.neiquanlib.utils.LogC;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 作者 ： hjb
 * 时间 ： 2016/7/25.
 */

public class PinyinDemoFragment extends BaseListViewFragment {


    private List<MyUser> users;

    @Override
    protected void setHeadViews() {
        users = new ArrayList<>();
        MyUser user = new MyUser();
        user.setNickName("胡歌");
        user.setHeadUrl("http://img5.imgtn.bdimg.com/it/u=3734691226,3860980271&fm=21&gp=0.jpg");
        users.add(user);
        MyUser user0 = new MyUser();
        user0.setNickName("赵家");
        user0.setHeadUrl("http://img5.imgtn.bdimg.com/it/u=3734691226,3860980271&fm=21&gp=0.jpg");
        users.add(user0);
        MyUser user1 = new MyUser();
        user1.setNickName("赵雨");
        user1.setHeadUrl("http://img2.imgtn.bdimg.com/it/u=2140290594,1541677148&fm=21&gp=0.jpg");
        users.add(user1);
        MyUser user7 = new MyUser();
        user7.setNickName("dasdjkj");
        user7.setHeadUrl("http://img5.imgtn.bdimg.com/it/u=437684409,2192186&fm=21&gp=0.jpg");
        users.add(user7);
        MyUser user2 = new MyUser();
        user2.setNickName("钱强");
        user2.setHeadUrl("http://img1.imgtn.bdimg.com/it/u=3048062991,2735177468&fm=21&gp=0.jpg");
        users.add(user2);
        MyUser user3 = new MyUser();
        user3.setNickName("孙悟");
        user3.setHeadUrl("http://img1.imgtn.bdimg.com/it/u=1493333668,4160867923&fm=11&gp=0.jpg");
        users.add(user3);
        MyUser user4 = new MyUser();
        user4.setNickName("李一");
        user4.setHeadUrl("http://img5.imgtn.bdimg.com/it/u=437684409,2192186&fm=21&gp=0.jpg");
        users.add(user4);
    }

    @Override
    protected boolean isHaveHead() {
        return false;
    }

    @Override
    protected MyBaseAdapterListView getAdapter() {
        return new PinyinAdapter_(getActivity(), null);
    }

    @Override
    public void initData() {
        /**
         * 这里是开始给的集合
         */
        for (MyUser user : users) {
            LogC.e("===MyUser---" + user.toString() + "----" + user.getInitialLetter());
            UserLetterUtils.setUserInitialLetter(user);
        }
        /**
         * 集合排序 这个方法后面再用这个集合就是排序之后的了
         */
        // sorting
        Collections.sort(users, new Comparator<MyUser>() {

            @Override
            public int compare(MyUser lhs, MyUser rhs) {
                if (lhs.getInitialLetter().equals(rhs.getInitialLetter())) {
                    return lhs.getNickName().compareTo(rhs.getNickName());
                } else {
                    if ("#".equals(lhs.getInitialLetter())) {
                        return 1;
                    } else if ("#".equals(rhs.getInitialLetter())) {
                        return -1;
                    }
                    return lhs.getInitialLetter().compareTo(rhs.getInitialLetter());
                }

            }
        });
        /**
         * hashmap是无序的,treemap 是有序的 所谓的有序无序是指添加顺序和取出顺序一致
         */
        TreeMap<String, List<MyUser>> myUserMap = new TreeMap<>();
        /**
         * 这里就是排序过后的集合 将这个集合里面的user放在字母中
         */
        for (MyUser myUser : users) {
            LogC.e("--users---" + myUser.toString() + "----" + myUser.getInitialLetter());
            String initialLetter = myUser.getInitialLetter();
            List<MyUser> myUsers;
            if (!myUserMap.containsKey(initialLetter)) {
                myUsers = new ArrayList<>();
                myUsers.add(myUser);
                myUserMap.put(initialLetter, myUsers);
            } else {
                myUsers = myUserMap.get(initialLetter);
                myUsers.add(myUser);
            }
        }
        List<AfterUser> afterUsers = new ArrayList<>();
        for (Map.Entry<String, List<MyUser>> entry : myUserMap.entrySet()) {
            String key = entry.getKey();
            List<MyUser> value = entry.getValue();
            AfterUser afterUser = new AfterUser();
            afterUser.setInitialLetter(key);
            afterUser.setUsers(value);
            afterUsers.add(afterUser);
        }
        adapter.append(afterUsers);
    }

    @Override
    public void onRetryClick() {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }
}
